package service

import (
	"api-wareway/basic/config"
	__ "api-wareway/basic/proto"
	"api-wareway/handle/request"
	"api-wareway/pkg"
	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
	"log"
	"net/http"
	"path/filepath"
	"strings"
)

func AddVideo(c *gin.Context) {
	var form request.AddVideoForm

	err := c.ShouldBind(&form)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "验证失败",
		})
		return
	}

	video, err := config.VideoClient.AddVideo(c, &__.AddVideoReq{
		UserId:    int64(form.UserId),
		Title:     form.Title,
		VideoType: form.VideoType,
		VideoTag:  form.VideoTag,
		ImageUrl:  form.ImageUrl,
		VideoUrl:  form.VideoUrl,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "发布失败",
			"data": err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "发布成功",
		"data": video,
	})

}

func UploadFile(c *gin.Context) {
	// 单文件
	file, _ := c.FormFile("file")
	etc := strings.ToTitle(filepath.Ext(file.Filename))

	file.Filename = uuid.NewString() + etc

	log.Println(file.Filename)

	url := ".././file" + file.Filename

	// 上传文件至指定的完整文件路径
	oss := pkg.UploadFileOss(url)
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "发布成功",
		"data": oss,
	})
}
